package com.myblog.controller;

import com.myblog.Utils.BeanCopyUtils;
import com.myblog.Utils.SecurityUtils;
import com.myblog.domain.GlobalResponse;
import com.myblog.domain.Vo.AdminUserInfoVo;
import com.myblog.domain.Vo.UserInfoVo;
import com.myblog.domain.contants.GlobalConstants;
import com.myblog.domain.entity.User;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.expection.GlobalException;
import com.myblog.services.*;
import org.omg.CORBA.SystemException;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.text.GapContent;
import java.util.List;

@RestController
public class LoginController {
    @Resource
    private AdminLogInService adminLogInService;
    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleService roleService;

    @PostMapping("/user/login")
    public GlobalResponse login(@RequestBody User user){
        if(!StringUtils.hasText(user.getUserName())){
            //提示 必须要传用户名
            throw new GlobalException(GlobalResponseEnum.REQUIRE_USERNAME);
        }
       return adminLogInService.Login(user);
    }

    
    @GetMapping("/getInfo")
    public GlobalResponse getInfo(){
        User LoginUser = adminLogInService.getById(SecurityUtils.getUserId());
        User user = userService.getById(LoginUser.getId());
        UserInfoVo userInfo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        List<String> roleList=roleService.getRoleList(LoginUser.getId());
        List<String> permissionsList=menuService.getPermissionList(LoginUser.getId());
        AdminUserInfoVo adminUserInfoVo=new AdminUserInfoVo(userInfo,roleList,permissionsList);
        return GlobalResponse.ok(adminUserInfoVo);
    }

    @GetMapping("/getRouters")
    public GlobalResponse getRouters(){
        return menuService.getRouters();
    }



}
